<template>
  <header class="header">
    <div class="container">
      <div class="logo">
        <router-link to="/">沸点技术</router-link>
      </div>
      <div class="nav-links">
        <ul>
          <li><router-link to="/" class="active">首页</router-link></li>
          <li><router-link to="/category">分类</router-link></li>
          <li><router-link to="/tags">标签</router-link></li>
          <li><router-link to="/archive">归档</router-link></li>
          <li><router-link to="/about">关于</router-link></li>
          <li v-if="userStore.isLogin"><router-link to="/publish">写文章</router-link></li>
        </ul>
      </div>
      <div class="right-section">
        <div class="search-box">
          <el-input v-model="searchText" placeholder="搜索文章..." clearable>
            <template #prefix>
              <el-icon><Search /></el-icon>
            </template>
          </el-input>
        </div>
        <div class="user-section">
          <template v-if="userStore.isLogin">
            <el-dropdown trigger="click">
              <div class="user-avatar">
                <el-avatar :size="36" :src="userStore.userInfo.avatar || defaultAvatar" />
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item>
                    <router-link to="/user">个人中心</router-link>
                  </el-dropdown-item>
                  <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
          <template v-else>
            <el-button type="primary" @click="showLogin" class="login-btn">登录 / 注册</el-button>
          </template>
        </div>
      </div>
    </div>
  </header>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { ElMessageBox } from 'element-plus'
import { useUserStore } from '../store/user'
import { useRouter } from 'vue-router'

const userStore = useUserStore()
const router = useRouter()
const searchText = ref('')

// 默认头像
const defaultAvatar = 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'

// 显示登录弹窗
function showLogin() {
  userStore.toggleLoginModal(true)
}

// 退出登录
function handleLogout() {
  ElMessageBox.confirm('确定要退出登录吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    userStore.clearUserInfo()
    router.push('/')
  }).catch(() => {})
}
</script>

<style scoped lang="scss">
.header {
  height: 64px;
  background-color: #fff;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  
  .container {
    max-width: 1200px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    height: 100%;
    padding: 0 20px;
  }
  
  .logo {
    font-size: 24px;
    font-weight: bold;
    margin-right: 40px;
    
    a {
      font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
      background-image: linear-gradient(to right, #2979ff, #1e88e5);
      -webkit-background-clip: text;
      color: transparent;
      text-decoration: none;
    }
  }
  
  .nav-links {
    flex: 1;
    
    ul {
      display: flex;
      list-style: none;
      margin: 0;
      padding: 0;
      
      li {
        margin-right: 20px;
        
        a {
          color: #606266;
          text-decoration: none;
          font-size: 16px;
          padding: 0 10px;
          position: relative;
          transition: all 0.3s;
          
          &:hover, &.router-link-active, &.active {
            color: #2979ff;
            
            &::after {
              content: '';
              position: absolute;
              bottom: -22px;
              left: 0;
              width: 100%;
              height: 3px;
              background-color: #2979ff;
            }
          }
        }
      }
    }
  }
  
  .right-section {
    display: flex;
    align-items: center;
    
    .search-box {
      width: 200px;
      margin-right: 20px;
      
      .el-input {
        --el-input-inner-height: 36px;
        
        .el-input__wrapper {
          border-radius: 18px;
        }
      }
    }
    
    .user-section {
      display: flex;
      align-items: center;
      
      .user-avatar {
        cursor: pointer;
      }
      
      .login-btn {
        border-radius: 18px;
        background: linear-gradient(to right, #2979ff, #1e88e5);
        border: none;
        
        &:hover {
          opacity: 0.9;
        }
      }
    }
  }
}
</style> 